package it.polimi.it.TravelDream.ejb.eaos;

import java.util.ArrayList;
import java.util.List;

import it.polimi.it.TravelDream.ejb.PackageMgr;
import it.polimi.it.TravelDream.ejb.UserMgr;
import it.polimi.it.TravelDream.ejb.eaos.PackageDTO;
import it.polimi.it.TravelDream.entities.Group;
import it.polimi.it.TravelDream.entities.PersonalizedPackage;
import it.polimi.it.TravelDream.entities.PredefinedPackage;
import it.polimi.it.TravelDream.entities.Purchas;
import it.polimi.it.TravelDream.entities.User;

import javax.annotation.Resource;
import javax.ejb.EJBContext;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class PackageMgrBean
 */
@Stateless
@LocalBean
public class PackageMgrBean implements PackageMgr {
	
	@PersistenceContext
    private EntityManager em;
   
	//riceve in ingresso la chiave di ricerca passatagli dal web
	//dovrebbe restituire la lista di pacchetti che soddisfare la query
	//metodo restituisce la lista di tutti i pacchetti presenti nel db.
	@Override
	public ArrayList<PackageDTO> search(String searchKeyword){
		return listConverterTDO( em.createNamedQuery(PredefinedPackage.FIND_ALL, PredefinedPackage.class)
                .getResultList());
	}
	
	@Override
	public void createNewPackage() {
		PredefinedPackage pack = new PredefinedPackage();
		em.persist(pack);
	}
	
	@Override
	public void changeBasicProduct(PackageDTO persPack, String employeeCreator ){
		System.out.println("*************************");
		//System.out.println(persPack.getIdbasicproduct1());
		//System.out.println(userCreator);
		System.out.println("*************************");
			em.merge(new PredefinedPackage(persPack, employeeCreator));
		}
	
	//converte tutta lista di pacchetti in pacchetti DTO
	private ArrayList<PackageDTO> listConverterTDO(List<PredefinedPackage>predPackList){
		int i;
		ArrayList<PackageDTO> newList = new ArrayList<PackageDTO>();
		for( i=0;i<predPackList.size();i++){
			newList.add(convertToDTO(predPackList.get(i)));
		}
		
		return newList;
		
	}
	
	//metodo che converte un pacchetto in un pacchettoDTO
	private PackageDTO convertToDTO(PredefinedPackage predPack) {
		PackageDTO packageDTO = new PackageDTO();
		packageDTO.setIdPackage(predPack.getIdpackage());
		packageDTO.setCreationData(predPack.getCreationdata());
		packageDTO.setEmployeeCreator(predPack.getEmployeecreator());
		packageDTO.setIdBasicProduct1(predPack.getIdbasicproduct1());
		packageDTO.setIdBasicProduct2(predPack.getIdbasicproduct2());
		packageDTO.setIdBasicProduct3(predPack.getIdbasicproduct3());
		packageDTO.setDiscount(predPack.getDiscount());
		packageDTO.setTotalPrice(predPack.getTotalprice());
		
		return packageDTO;
	}
    /**
     * Default constructor. 
     */
    public PackageMgrBean() {
        // TODO Auto-generated constructor stub
    }

	//@Override
	public void printname(){
		System.out.println("***************************************************** ciao fabio****************");
	}
	
	//aggiunge il pacchetto alla lista degli acquisti e rimuove il pacchetto dalla lista di pacchetti predefiniti
	@Override
	public void purchase(PackageDTO pack){
		String emailBuyer = pack.getIdBuyer() ;
		System.out.println("***************************");
		System.out.println(emailBuyer);
		Purchas purchase = new Purchas(pack,emailBuyer);
		em.persist(purchase);
		remove(pack);
		
	}
	//riceve idPackage, lo trova e lo elimina dal database;
	 public void remove(PackageDTO pack2) {
			PredefinedPackage pack = find(pack2.getIdPackage());
	        em.remove(pack);
		}

	 // trova il pacchetto nel database attraverso la sua chiave primaria
	 public PredefinedPackage find(String pack2) {
	    	return em.find(PredefinedPackage.class, pack2);
	    }
	
	
	
}
